#include "stdafx.h"
#include "ScoreManagement.h"
#include "Takes.h"
#include "TakesDB.h"
#include "Grade.h"
#include "GradeDB.h"


ScoreManagement::ScoreManagement()
{
}

Grade ScoreManagement::StuCheckOneScore(const string &id, const string &course)//学生查看单个课程成绩
{
	TakesDB tDB;
	GradeDB gDB;
	Takes t = tDB.selectByStudentIdAndCourseId(id, course);
	string gid = t.getGradeId();
	cout << gid;
	Grade grade = gDB.selectByGradeId(gid);
	return grade;
}

list<Grade> ScoreManagement::StuCheckAllScore(const string &id)//学生查看全部课程成绩
{
	TakesDB tDB;
	GradeDB gDB;
	list<Takes> takes = tDB.selectAllByStudentId(id);
	list<string> gid;
	for each(Takes t in takes)
	{
		string g = t.getGradeId();
		gid.push_back(g);
	}
	list<Grade> grade;
	for each(string g in gid)
	{
		Grade gg = gDB.selectByGradeId(g);
		grade.push_back(gg);
	}
	return grade;
}

Grade ScoreManagement::TeaCheckOneScore(const string &stu_id, const string &course_id)//老师查看某课程单个学生成绩
{
	TakesDB tDB;
	GradeDB gDB;
	Takes takes = tDB.selectByStudentIdAndCourseId(stu_id, course_id);
	Grade grade = gDB.selectByGradeId(takes.getGradeId());
	return grade;
}

list<Grade> ScoreManagement::TeaCheckAllScore(const string &course_id)//老师查看某课程所有学生成绩
{
	TakesDB tDB;
	GradeDB gDB;
	list<Takes> takes = tDB.selectAllByCourseId(course_id);
	list<string> gid;
	for each(Takes t in takes)
	{
		string g = t.getGradeId();
		gid.push_back(g);
	}
	list<Grade> grade;
	for each(string g in gid)
	{
		Grade gg = gDB.selectByGradeId(g);
		grade.push_back(gg);
	}
	return grade;
}

list<double> ScoreManagement::TeaAnalysisScore(const string course_id)//老师查看课程成绩统计分析
{
	//最大值、最小值、平均值、优秀率、良好率、不及格率
	TakesDB tDB;
	GradeDB gDB;
	list<Takes> takes = tDB.selectAllByCourseId(course_id);
	list<string> gid;
	list<double> analysis;
	for each(Takes t in takes)
	{
		string g = t.getGradeId();
		gid.push_back(g);
	}
	if (gid.size() == 0)
	{//成绩为空时不进行统计
		return analysis;
	}
	list<Grade> grade;
	for each(string g in gid)
	{
		Grade gg = gDB.selectByGradeId(g);
		grade.push_back(gg);
	}
	list<double> total_mark;
	for each(Grade g in grade)//得到全部该课程所有成绩
	{
		double t = g.getTotal();
		total_mark.push_back(t);
	}

	double max = total_mark.front(), min = total_mark.front(), avg, excellent, good, fail, sum = 0, e_sum = 0, g_sum = 0, f_sum = 0;
	//typedef list<double> name;
	list<double>::iterator i;//iterator迭代器
	for (i = total_mark.begin(); i != total_mark.end(); i++)
	{
		if (max < *i)//求max
			max = *i;
		if (min > *i)//求min
			min = *i;
		sum += *i;//求总数以获得avg
		if (*i >= 90)//计算优秀的人数
			e_sum++;
		else if (*i >= 80)//计算良好的人数
			g_sum++;
		else if (*i < 60)//计算不及格的人数
			f_sum++;
	}
	cout << "sum:" << sum << endl;
	cout << "final_mark.size():" << total_mark.size() << endl;
	avg = sum / (double)total_mark.size();
	excellent = e_sum / (double)total_mark.size();
	good = g_sum / (double)total_mark.size();
	fail = f_sum / (double)total_mark.size();
	if (max == 0 || avg == 0) {
		return analysis;
	}
	analysis.push_back(max);
	analysis.push_back(min);
	analysis.push_back(avg);
	analysis.push_back(excellent);
	analysis.push_back(good);
	analysis.push_back(fail);
	return analysis;//把最大值、最小值、平均值、优秀率、良好率、不及格率按顺序放到list数组中当成返回值

}

string ScoreManagement::TeaAddStuScore(const string &stu_id, const string &course_id, double ordinary, double final)//老师添加某课程学生成绩
{
	//grade_id是把系统时间转换成字符串而得的
	SYSTEMTIME sys;
	GetLocalTime(&sys);
	string grade_id = to_string(sys.wYear) + to_string(sys.wMonth) + to_string(sys.wDay) + to_string(sys.wHour) + to_string(sys.wMinute) + to_string(sys.wSecond) + to_string(sys.wMilliseconds);
	TakesDB tDB;
	GradeDB gDB;
	Takes t(stu_id, course_id, grade_id);
	//tDB.insert(t);
	tDB.update(t);
	double total;
	if (final >= 60)
		total = ordinary*0.4 + final*0.6;
	else
		total = final;
	Grade g(grade_id, ordinary, final, total);
	gDB.insert(g);
	return "已成功添加该学生这门课程的成绩！";
}

string ScoreManagement::TeaModifyStuScore(const string &stu_id, const string &course_id, double ordinary, double final)//老师修改某课程学生成绩
{
	GradeDB gDB;
	TakesDB tDB;
	Takes t = tDB.selectByStudentIdAndCourseId(stu_id, course_id);
	string gid = t.getGradeId();
	double total;
	if (final >= 60)
		total = ordinary*0.4 + final*0.6;
	else
		total = final;
	Grade g(gid, ordinary, final, total);
	gDB.update(g);
	return "已成功修改该学生该课程的成绩!";
}

string ScoreManagement::TeaDeleteStuScore(const string &stu_id, const string &course_id)//老师删除某课程学生成绩
{
	GradeDB gDB;
	TakesDB tDB;
	Takes t = tDB.selectByStudentIdAndCourseId(stu_id, course_id);
	string gid = t.getGradeId();
	gDB.deleteByGradeId(gid);
	t.setGradeId("");
	tDB.update(t);
	return "已成功删除该学生这门课程的成绩！";
}


ScoreManagement::~ScoreManagement()
{
}
